package com.fy56.platform.logistics.restful.api.controller.wx;


import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.model.VipBank;
import com.fy56.platform.logistics.restful.api.app.authorization.annotation.Authorization;
import com.fy56.platform.logistics.service.wx.IVipBankService;
import com.fy56.platform.logistics.utils.StringUtil;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * VIP用户银行RestController
 * Created by li on 2018-01-29.
 */
@Api(value = "VipBankManager", description = "银行管理", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/bankInfo")
public class VipBankController {

	@Autowired
	private IVipBankService iVipBankService;

    private static Logger log = Logger.getLogger(VipBankController.class);

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ApiOperation(value = "查询银行信息")
    @Authorization
    public ResponseEntity search(@RequestParam String vipMemberId) {
        log.info("用户ID：" + vipMemberId);
        try {
            return new ResponseEntity(ResultModel.ok(iVipBankService.select(Long.valueOf(vipMemberId))), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询银行信息失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "查询银行信息失败"), HttpStatus.OK);
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加银行信息")
    @Authorization
    public ResponseEntity add(@RequestParam String data) {
        VipBank model = JSON.parseObject(data).toJavaObject(VipBank.class);
        if(model != null && StringUtil.isNotEmpty(model.getBankCardNo())){
            List<VipBank> list = iVipBankService.selectByCardNo(model);
            if(list != null && list.size() > 0){
                return new ResponseEntity(ResultModel.error("银行卡号[" + model.getBankCardNo() + "]已存在"), HttpStatus.OK);
            }
        }
        try {
            return new ResponseEntity(ResultModel.ok(iVipBankService.insert(model)), HttpStatus.OK);
        } catch (Exception e) {
            log.error("添加银行信息失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "添加银行信息失败"), HttpStatus.OK);
        } finally {
            model = null;
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除银行信息")
    @Authorization
    public ResponseEntity delete(@RequestParam String id) {
        log.info("银行卡ID：" + id);
        try {
            return new ResponseEntity(ResultModel.ok(iVipBankService.delete(Long.valueOf(id))), HttpStatus.OK);
        } catch (Exception e) {
            log.error("删除银行信息失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "删除银行信息失败"), HttpStatus.OK);
        }
    }

}